package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Product;

import java.util.List;

/**
 * 商品的Dao层
 */
public class ProductDao extends BaseDao<Product,Integer> {

    /*---------------------商品添加功能 开始---------------------*/
    /**
     * 添加商品
     * @param product
     * @return
     */
    @Override
    public int save(Product product) {
        String sql = "INSERT INTO `transaction`.`product`(`classify_id`, `product_name`, " +
                "`product_allnumber`, `product_price`, `product_picture`, `product_particulars`, `merchant_id`)" +
                " VALUES (?,?,?,?,?,?,1)";
        Object[] objects = {product.getClassifyId(),product.getProductName(),
                product.getProductAllnumber(),product.getProductPrice(),product.getProductPicture(),
                product.getProductParticulars()};
        return helper.executeUpdate(sql,objects);
    }
    /**
     * 添加商品之前要查看商品名称是否已经存在。
     * 这个方法不是重写的。
     * @param productName
     * @return
     */
    public Product findByProductName(String productName){
        String sql = "select product_name,product_allnumber,product_price,product_picture," +
                "product_particulars,classify_id from product where product_name=?";
        List<Product> list = helper.executeQuery(sql,Product.class,productName);
        //设product为空，如果找不到正好直接return空。
        Product product = null;
        if (list.size()>0){
            product = list.get(0);
        }
        return product;
    }
    /*---------------------商品添加功能 结束---------------------*/

    /*---------------------商品分页查询功能 开始---------------------*/
    /**
     * 分页查询商品
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<Product> findListByPage(int pageIndex,int pageSize){
        String sql = "select product_id,product_name,product_allnumber,product_price," +
                "product_picture,product_particulars,merchant_id,c.classify_name from product p" +
                " inner join classify c on p.classify_id=c.classify_id limit ?,?";
        int index = (pageIndex-1)*pageSize;
        //最终查的是：0 5 ，5 5 ，10 5   pageSize=5，pageSize自己定。
        return helper.executeQuery(sql,Product.class,index,pageSize);
    }
    /**
     * 统计总条数,分页用到。
     * @return
     */
    public int count(){
        String sql = "select count(*) from product";
        return helper.executeScale(sql);
    }
    /**
     * 商家查出自己的商品，
     * @param merchantId
     * @return
     */
    public List<Product> findListByMerchantId(int merchantId,int pageIndex,int pageSize){
        String sql = "select product_id,product_name,product_allnumber,product_price," +
                "product_picture,product_particulars,merchant_id,c.classify_name from product p" +
                " inner join classify c on p.classify_id=c.classify_id where merchant_id=? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        return helper.executeQuery(sql,Product.class,merchantId,index,pageSize);
    }
    /**
     * 统计总条数,商家查出自己的商品用到。
     * @return
     */
    public int count(int merchantId){
        String sql = "select count(*) from product where merchant_id=?";
        return helper.executeScale(sql,merchantId);
    }
    /**
     * 模糊查询用到。
     * @param merchantId
     * @param searchContent
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<Product> findProductListByProductNameLike(int merchantId,String searchContent,int pageIndex,int pageSize){
        String sql = "select product_id,product_name,product_allnumber,product_price,product_picture, " +
                "product_particulars,merchant_id,c.classify_name from product p inner join " +
                "classify c on p.classify_id=c.classify_id where merchant_id=? and product_name " +
                "like ? limit ?,?";
        //有四个条件：merchant_id，搜索的内容，0，5
        int index = (pageIndex-1)*pageSize;
        return helper.executeQuery(sql,Product.class,merchantId,"%"+searchContent+"%",index,pageSize);
    }
    /**
     * 统计总条数,模糊查询用到。
     * @return
     */
    public int count(int merchantId,String searchContent,int pageIndex,int pageSize){
        String sql = "select count(*) from product where merchant_id=? and product_name " +
                "like ? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        return helper.executeScale(sql,merchantId,"%"+searchContent+"%",index,pageSize);
    }
    /*---------------------商品分页查询功能 结束---------------------*/

    /*---------------------商品修改功能 开始---------------------*/
    /**
     * 根据id查询分类，修改前需要回显数据，要显示该商品之前是什么类别的
     * @param id
     * @return
     */
    public Product findClassifyById(Integer id){
        String sql = "select product_id,product_name,product_allnumber,product_price," +
                "product_picture,product_particulars,merchant_id,c.classify_id,c.classify_name from product p" +
                " inner join classify c on p.classify_id=c.classify_id where product_id=?";
        List<Product> list = helper.executeQuery(sql,Product.class,id);
        Product product = null;
        if (list.size()>0){
            product = list.get(0);
        }
        return product;
    }
    /**
     * 商品修改
     * @param product
     * @return
     */
    public int updateProduct(Product product){
        String sql = "UPDATE `transaction`.`product` SET `classify_id` = ?," +
                "`product_name` = ?,`product_allnumber` = ?,`product_price` = ?," +
                "`product_picture` = ?,`product_particulars` = ? WHERE `product_id`=?";
        Object[] objects = {product.getClassifyId(),product.getProductName(),
                product.getProductAllnumber(),product.getProductPrice(),
                product.getProductPicture(),product.getProductParticulars(),
                product.getProductId()};
        return helper.executeUpdate(sql,objects);
    }
    /*---------------------商品修改功能 结束----------------------*/

    /*---------------------商品删除(物理删除)功能 开始---------------------*/
    /**
     * 商品删除，通过商品id物理删除。
     * @param productId
     * @return
     */
    public int deleteProductById(Integer productId){
        String sql = "delete from product where product_id=?";
        return helper.executeUpdate(sql,productId);
    }
    /*---------------------商品删除(物理删除)功能 结束---------------------*/


    /**
     * 根据分类id查找商品
     *  @param classifyId
     *  @return
     */
    public List<Product> findProductByClassifyId(Integer classifyId){
        String sql = "select product_id from product where classify_id = ? ";
        return helper.executeQuery(sql,Product.class,classifyId);
    }
    /**
     * 根据Id查找商品
     */
    @Override
    public Product findById(Integer integer) {
        String sql="select product_id,product_name,product_allnumber,product_price, " +
                " product_picture,product_particulars from product where product_id=?";

       List<Product> products=helper.executeQuery(sql,Product.class,integer);
       Product product=products.get(0);
       return product;
    }
    /**
     * 查询所有的商品
     */
    public List<Product> findAll(){
        String sql="select product_id,product_name,product_allnumber,product_price, " +
                "  product_picture,product_particulars from product";
        return helper.executeQuery(sql,Product.class);
    }
}
